<template>
  <div class="card">
    <div
      v-for="(item, index) in cardOpts"
      :key="item.key"
      class="card__item"
      :style="{ 'min-width': 100 / colNum + '%' }"
      :class="{ 'no-padding': (index + 1) % colNum === 0 }"
    >
      <div class="card__wrapper">
        <div class="card__header">
          <svg-icon :icon-class="item.icon" class="icon" :style="{ color: item.color }" />
          <span class="title">
            {{ item.text }}
          </span>
        </div>
        <div class="card__body">
          <span class="value">
            <countTo
              :start-val="item.startVal"
              :end-val="item.endVal || 0"
              :decimals="item.decimals"
              :suffix="item.suffix"
              :prefix="item.prefix"
            />
          </span>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
  import countTo from 'vue-count-to'
  export default {
    name: 'CardPanel',
    components: { countTo },
    props: {
      cardOpts: {
        type: Array,
        default() {
          return []
        }
      },
      colNum: {
        type: Number,
        default: 5
      }
    },
    data() {
      return {
        flag: true
      }
    },
    computed: {
      device() {
        return this.$store.state.app.device
      }
    },
    watch: {
      cardOpts: {
        deep: true,
        handler() {
          this.flag = true
          setTimeout(() => {
            this.flag = false
          }, 10000)
        }
      }
    },
    methods: {
      getResultVal(start = 0, end = 0) {
        const startVal = start
        const endVal = end
        const resultVal = endVal - startVal
        if (this.isInteger(resultVal)) {
          return resultVal
        }
        return (endVal - startVal).toFixed(2)
      },
      isInteger(obj) {
        return typeof obj === 'number' && obj % 1 === 0
      }
    }
  }
</script>

<style lang="scss" scoped>
  .card {
    display: inline-flex;
    width: 100%;
    justify-content: flex-start;
    align-content: center;
    flex-direction: row;
    flex-wrap: wrap;
    &__item {
      color: #fff;
      padding-right: 20px;
      cursor: pointer;
      margin-bottom: 20px;
    }
    &__wrapper {
      border-radius: 2px;
      height: 100px;
      box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
      background-color: #fff;
    }

    .card__header {
      padding: 10px;
      .icon {
        font-size: 24px;
      }
      .title {
        font-size: 14px;
        font-weight: 500;
        color: #898c94;
        word-spacing: nowarp;
      }
    }
    .card__body {
      padding-left: 10px;
      margin-top: 20px;
      color: #303133;
      .value {
        font-size: 18px;
        font-weight: 700;
      }
    }
    .no-padding {
      padding-right: 0px;
    }
  }
  .mobile {
    .card {
      display: block;
      // padding: 10px;
      &__item {
        width: 100%;
        padding: 0;
      }
      &__wrapper {
        border-radius: 5px;
        margin-bottom: 10px;
      }
    }
  }
</style>
